package com.boarsoft.message.bean;

import java.io.Serializable;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.boarsoft.message.store.MessageRoller;

public class MessageEntity implements Comparable<MessageEntity>, Serializable {
	private static final long serialVersionUID = -4952888504955356871L;

	private static final Logger log = LoggerFactory.getLogger(MessageEntity.class);

	/** */
	protected Message message;
	/** */
	protected MessageRoller roller;

	protected long addTime;
	protected long lastTime;
	protected short status;
	protected int tried;
	protected String memo;
	protected Object content;

	@Override
	public int compareTo(MessageEntity m) {
		if (m == null) {
			return -1;
		}
		if (m.addTime > addTime) {
			return 1;
		} else if (m.addTime == addTime) {
			return 0;
		}
		return -1;
	}

	public MessageEntity() {
	}

	public MessageEntity(Message message) {
		this.message = message;
	}

	public MessageEntity(Message message, MessageRoller roller) {
		this.message = message;
		this.roller = roller;
	}

	public String toString() {
		return message.getId();
	}

	public void persist() throws Exception {
		if (roller == null) {
			return;
		}
		this.roller.append(message);
	}

	public void setStatus(short status, String memo) throws Exception {
		message.setStatus(status);
		if (roller == null) {
			return;
		}
		message.setStatus(status);
		roller.update(message, memo);
		// roller.update(message.getId(), status, message.getTried(), memo);
	}

	/**
	 * 消息状态更新策略：<br>
	 * 0: 全部成功才成功（有一个失败都算失败） 1: 全部失败才失败（有一个成功就算成功） 2: 总是成功（忽略所有收件人的接收状态）
	 * 
	 * @param recipient
	 * @param status
	 *            RecipeintStatus.status
	 * @param memo
	 * @throws Exception
	 */
	public void setRecipientStatus(String recipient, short status, int tried, String memo) throws Exception {
		// 下面两句需要同步，避免消息状态一直是STATUS_SENDING的bug
		synchronized (message) {
			// 更新收件人的状态
			message.setRecipientStatus(recipient, status, tried, memo);
			// 检查message的状态
			this.status = message.status();
		}
		this.lastTime = new Date().getTime();
		log.debug("Message status {} = {}", message, this.status);

		// 如果没有持久化层，或者消息仍未完成（还有收件人消息没有处理过）
		if (roller == null) {
			return;
		}
		if (message.getRecipients().size() > 1) {
			roller.update(message.getId(), recipient, tried, status, memo);
		}
		// 避免重复update
		if (this.status == Message.STATUS_SENDING) {
			return;
		}
		message.setStatus(this.status);
		roller.update(message, memo);
	}

	public int tried1() {
		this.tried++;
		message.setTried(this.tried);
		return this.tried;
	}

	public int tried1(String recipient) {
		return message.tried1(recipient);
	}

	public int getTried() {
		return message.getTried();
	}

	public int getTried(String recipient) {
		return message.getTried(recipient);
	}

	public Message getMessage() {
		return message;
	}

	public short getStatus() {
		return status;
	}

	public void setStatus(short status) {
		this.status = status;
		message.setStatus(status);
	}

	public MessageRoller getRoller() {
		return roller;
	}

	public void setRoller(MessageRoller roller) {
		this.roller = roller;
	}

	public long getAddTime() {
		return addTime;
	}

	public void setAddTime(long addTime) {
		this.addTime = addTime;
	}

	public long getLastTime() {
		return lastTime;
	}

	public void setLastTime(long lastTime) {
		this.lastTime = lastTime;
	}

	public void setMessage(Message message) {
		this.message = message;
	}

	public String getMemo() {
		return memo;
	}

	public void setTried(int tried) {
		this.tried = tried;
	}

	public void setMemo(String memo) {
		this.memo = memo;
	}
}
